package me.monster.smarttraffic.ui.dash;

import android.os.Environment;

import com.orhanobut.logger.Logger;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import me.monster.smarttraffic.R;
import me.monster.smarttraffic.base.BasePresenter;
import me.monster.smarttraffic.bo.DataHolder;
import me.monster.smarttraffic.net.RemoteService;
import me.monster.smarttraffic.tool.Util;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-11 16:24
 */
public class DashPresenter extends BasePresenter<DashContract.View> implements DashContract.Presenter {
    private static final String TAG = "DashPresenter";

    @Override
    public void fetchInfo() {
        RemoteService remoteService = RemoteService.getInstance();
        Disposable dashFetchDis = Observable.zip(remoteService.fetchRoadEnvironment(), remoteService.fetchBusDistance(), new BiFunction<Map<String, String>, Map<String, String>, Map<String, String>>() {

            @Override
            public Map<String, String> apply(Map<String, String> roadMap, Map<String, String> busMap) throws Exception {
                Map<String, String> map = new HashMap<>(roadMap.size() + busMap.size());
                map.putAll(roadMap);
                map.putAll(busMap);
                return map;
            }
        }).subscribe(new Consumer<Map<String, String>>() {
            @Override
            public void accept(Map<String, String> stringStringMap) throws Exception {
                iView.updateInfo(stringStringMap);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                iView.toast(handleError(throwable));
            }
        });
        addDispose(dashFetchDis);
    }

    @Override
    public void copyVideo() {
        final DataHolder instance = DataHolder.getInstance();
        if (instance.getVideoPath().endsWith("sampleVideo.mp4")) {
            return;
        }
        InputStream inputStream = Util.getApp().getResources().openRawResource(R.raw.a4);
        File movieFile = Util.getApp().getExternalFilesDir(Environment.DIRECTORY_MOVIES);
        try (FileOutputStream fileOutputStream = new FileOutputStream(new File(movieFile, "/sampleVideo.mp4"));
             final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
            int len = -1;
            byte[] buffer = new byte[1024 * 10];
            while ((len = inputStream.read(buffer)) != -1) {
                bufferedOutputStream.write(buffer, 0, len);
            }
            bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        instance.setVideoDone(true);
        if (movieFile == null) {
            return;
        }
        File[] files = movieFile.listFiles();
        if (files != null) {
            instance.setVideoFileArray(files);
        }
        Logger.d("视频文件为：" + instance.getVideoPath());
    }
}
